
import 'package:flutter/material.dart';
import 'package:flutter_trip/models/common_model.dart';
import 'package:flutter_trip/models/grid_nav_model.dart';
import 'package:flutter_trip/widget/webview.dart';

class GridNav extends StatelessWidget{
  final GridNavModel gridNavModel;
  final String name;

  GridNav(this.gridNavModel,{this.name='xiaoming'});

  @override
  Widget build(BuildContext context) {
     return PhysicalModel(
         child: Column(
       children: _gridNavItems(context),
         ),
       color: Colors.transparent,
       borderRadius: BorderRadius.circular(6),
       clipBehavior: Clip.antiAlias,
     );
  }
  _gridNavItems(BuildContext context){
    List<Widget> items =[];
    if(gridNavModel == null) return items;

    if(gridNavModel.hotel!=null){
      items.add(_gridNavItem(context,gridNavModel.hotel,true));


    }
    if(gridNavModel.flight!=null){
      items.add(_gridNavItem(context,gridNavModel.flight,false));

    }
    if(gridNavModel.travel!=null){
      items.add(_gridNavItem(context,gridNavModel.travel,false));

    }

    return items;
  }
  _gridNavItem(BuildContext context,GridNavItem item,bool isfirst){
    List<Widget> items =[];
    items.add(_mainItem(context, item.mainItem));
    items.add(_doubleItem(context, item.item1, item.item2,));
    items.add(_doubleItem(context, item.item3, item.item4,));

    List<Widget> exandItems = [];

    items.forEach((item) {
      exandItems.add(Expanded(
          child: item,flex: 1,)

      );
    });
    Color startColor = Color(int.parse('0xff'+item.startColor));
    Color endColor = Color(int.parse('0xff'+item.endColor));
    return Container(
      height: 88,
      margin: isfirst?null:EdgeInsets.only(top: 3),
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [startColor,endColor]),
      ),
      child: Row(children: exandItems,),
    );

  }
  _mainItem(BuildContext context,CommonModel model){
    return _wrapGesture(context, Stack(
      alignment: AlignmentDirectional.topCenter,
      children: [
        Image.network(model.icon,
          fit: BoxFit.contain,
          height: 88,
          width: 121,
          alignment: AlignmentDirectional.bottomEnd,
        ),
        Container(
          margin: EdgeInsets.only(top: 10),
          child: Text(model.title,style: TextStyle(fontSize: 14,color: Colors.white),
        ),)
      ],
    ), model);


  }
  _doubleItem(BuildContext context,CommonModel topModel,CommonModel bottomModel){
    return Column(
      children: [
        Expanded(
          child: _item(context, topModel, true, true),
        ),

        Expanded(
          child: _item(context, bottomModel, false, true),
        ),
      ],
    );
  }
  _item(BuildContext context,CommonModel item,isFirst,bool isCententItem,){
    BorderSide borderSide = BorderSide(width: 0.8,color: Colors.white);
    return FractionallySizedBox(
      widthFactor: 1,
      child: Container(
        decoration: BoxDecoration(
          border: Border(
            left:borderSide,
            bottom: isFirst?borderSide:BorderSide.none
          ),

        ),
        child:_wrapGesture(context, Center(
          child: Text(item.title,
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.white,fontSize: 14),
          ),
        ) , item)
      ),
    );
  }
  _wrapGesture(BuildContext context, Widget widget,CommonModel model){
    return GestureDetector(
      child: widget,
      onTap: (){
        Navigator.push(context, MaterialPageRoute(builder: (context)=>MyWebView(model.url, model.statusBarColor, model.title, model.hideAppBar)));

      },
    );
  }
}